package com.icez.base_util_library

/**
 *  数字工具类
 * 
 * @author Icez
 */
class NumberUtil {
    companion object {
        val instance: NumberUtil by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            NumberUtil()
        }
    }

    /**
     *  字符串转double类型
     * 
     * @author Icez
     * @param str 字符串
     */
    fun stringToDouble(str: String?): Double {
        return try {
            if (str.isNullOrEmpty()) {
                0.0
            } else {
                str.toDouble()
            }
        } catch (e: Exception) {
            0.0
        }

    }

    /**
     *  字符串转Int
     * 
     * @author Icez
     * @param str 字符串
     */
    fun stringToInt(str:String?):Int{
        return try{
            if(str.isNullOrEmpty()){
                0
            }else{
                str.toInt()
            }
        }catch (e:Exception){
            0
        }
    }
    
    /**
     *  字符串转Float
     *  
     * @author Icez
     * @param str 字符串
     */
    fun stringToFloat(str:String?):Float{
        return try{
            if(str.isNullOrEmpty()){
                0f
            }else{
                str.toFloat()
            }
        }catch (e:Exception){
            0f
        }
    }
    
    /**
     *  字符串转Long
     *  
     * @author Icez
     */
    fun stringToLong(str:String?):Long{
        return try{
            if(str.isNullOrEmpty()){
                0L
            }else{
                str.toLong()
            }
        }catch (e:Exception){
            0L
        }
    }
}